#!/bin/bash
set -eEuC
set -o pipefail

SPATH="$(readlink -f "$0")"
SDIR="$(dirname "${SPATH}")"; export SDIR;

# shellcheck source=common.sh
. "${SDIR}/common.sh"

publish_script() {
  local BRANCH_NAME; BRANCH_NAME="${1}"
  local SCRIPT_NAME; SCRIPT_NAME="${2}"
  local STABLE_VERSION;

  git checkout "${BRANCH_NAME}"

  if [[ ! -f "${SCRIPT_NAME}" ]]; then echo "${SCRIPT_NAME} not found" >&2; return; fi

  STABLE_VERSION="$(get_stable_version)"
  write_and_upload "${SCRIPT_NAME}" "${STABLE_VERSION}"
}

main() {
  setup

  get_version_file_and_lock

  while read -r tag; do
    publish_script "${tag}" install_asm
    publish_script "${tag}" asm_vm
  done <<EOF
$(all_release_tags)
EOF

  upload_version_file_and_unlock
}

main
